<style type="text/css">
.protected div { background: #f1cbcb url(<?php echo $cosa->system('modules-url').'/'.$m; ?>/img/locked.gif) no-repeat center right !important; }
</style>


<div class="moduleTopNav clearfix">
	<div class="fl-l">
		<?php $module->printAddLink($lang->get('add-nav')); ?>
	</div>
	<div class="fl-r"></div>
</div>

<div id="nav_items">
	<?php
	
	// global nav array
		$navs = array();
	
	// print items or show error
		$hasItems = printSubNavs(0);
		if(!$hasItems) $lang->put('no-items-found');
	
	// save button
		if($hasItems) echo '<br /><input type="button" onclick="javascript:savePositions()" value="'.$lang->get('nav-save').'" />';
	
	// recursive function
		function printSubNavs($parent_id, $protected=false) {
			
			// get navs
				global $cosa, $db, $lang, $m, $module, $oddEven;
				if($parent_id == 0) {
					$sql = "SELECT id, title, type, page_original_id, ext_url, ext_target, protected FROM ".$db->module('pages_nav')." WHERE (parent_id='".$parent_id."' OR type='nav') AND language_id='".$lang->id."' ORDER BY weight";
				} else {
					$sql = "SELECT id, title, type, page_original_id, ext_url, ext_target, protected FROM ".$db->module('pages_nav')." WHERE parent_id='".$parent_id."' AND language_id='".$lang->id."' ORDER BY weight";
				}
				$res = mysql_query($sql) or $cosa->oops(mysql_error()); //'Could not fetch navs.', 'db');
			
			// loop content
				if(!mysql_num_rows($res)) return false;
				else {
					
					// add to global array
						global $navs;
						$currId = 'sort_'.$parent_id;
						$navs[] = $currId;
					
					// generate ul
						echo '<ul id="'.$currId.'" class="sortable">'."\n";
						while($row = mysql_fetch_array($res)) {
							?>
							<li id="<?php echo stripslashes($row["id"]); ?>"<?php if($row['protected'] || $protected) echo ' class="protected"'; ?>>
								<div class="clearfix">
									<ol>
										<li class="icons">
											<?php
											$module->printEditLink($row['id']);
											$module->printDelLink($row['id']);
											?>
										</li>
										<li><strong><?php echo stripslashes($row['title']); ?></strong></li>
										<li><?php
											if($row['type'] == 'int') {
												$page = $module->pages_get_published($row['page_original_id']);
												echo '<a href="'.$cosa->system('base-url').'/?m='.$m.'&s=pages&edit='.$page['id'].'">'.stripslashes($page['title']).'</a>';
											} else if($row['type'] == 'ext') {
												echo '<a href="'.stripslashes($row['ext_url']).'">'.stripslashes($row['ext_url']);
												if($row['ext_target']) echo ' ('.stripslashes($row['ext_target']).')';
												echo '</a>';
											}
											?>
										</li>
									</ol>
								</div>
								<?php printSubNavs($row['id'], $row['protected']); ?>
							</li>
							<?php
						}
						echo '</ul>'."\n";
					
				}
			
			// return success
				return true;
			
		}
	
	?>
</div>

<script type="text/javascript">

// init sortable
	$(document).ready(function() {
		<?php
		foreach($navs as $k => $v) {
			foreach($navs as $k2 => $v2) {
				echo '$("#'.$v.'").sortable({ connectWith: ["#'.$v2.'"], revert: true });'."\n\t";
			}
			echo "\n\t";
		}
		?>
	});

// fetch positions
	function savePositions() {
		
		// fetch positions
			var p = '&p=1&m=<?php echo $m; ?>';
			$('#nav_items ul').each(function() {
				var t = $(this);
				var id = t.attr('id');
				var s = t.sortable('toArray');
				p += '&'+id+'='+s;
			});
		
		// do ajax call
			var url = '<?php echo $cosa->system('modules-url').'/'.$m.'/view/'; ?>nav_save.php';
			$.post(url, p, function(r) { showMessage(r); });
		
	}

</script>
